package com.draven.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 验证码工具类
 */
public class CaptchaUtil {

    // 随机字符集
    private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    // 验证码长度
    private static final int CODE_LENGTH = 4;
    // 图片宽度
    private static final int WIDTH = 100;
    // 图片高度
    private static final int HEIGHT = 40;
    // 干扰线数量
    private static final int INTERFERENCE_LINE_COUNT = 20;
    // 字体大小
    private static final int FONT_SIZE = 22;
    
    private static final Random RANDOM = new Random();

    /**
     * 生成验证码图片
     *
     * @return 包含验证码图片和验证码文本的对象
     */
    public static CaptchaInfo generateCaptcha() {
        // 创建图片缓冲区
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        
        // 设置背景色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        
        // 设置字体
        g.setFont(new Font("Arial", Font.BOLD, FONT_SIZE));
        
        // 绘制干扰线
        drawInterferenceLines(g);
        
        // 生成随机验证码
        String code = generateRandomCode();
        
        // 绘制验证码
        drawCode(g, code);
        
        g.dispose();
        
        return new CaptchaInfo(image, code);
    }
    
    /**
     * 生成随机验证码
     *
     * @return 随机验证码
     */
    public static String generateRandomCode() {
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < CODE_LENGTH; i++) {
            code.append(CHARS.charAt(RANDOM.nextInt(CHARS.length())));
        }
        return code.toString();
    }
    
    /**
     * 绘制干扰线
     *
     * @param g 图形上下文
     */
    private static void drawInterferenceLines(Graphics g) {
        for (int i = 0; i < INTERFERENCE_LINE_COUNT; i++) {
            int x1 = RANDOM.nextInt(WIDTH);
            int y1 = RANDOM.nextInt(HEIGHT);
            int x2 = RANDOM.nextInt(WIDTH);
            int y2 = RANDOM.nextInt(HEIGHT);
            
            g.setColor(getRandomColor());
            g.drawLine(x1, y1, x2, y2);
        }
    }
    
    /**
     * 绘制验证码
     *
     * @param g    图形上下文
     * @param code 验证码
     */
    private static void drawCode(Graphics g, String code) {
        for (int i = 0; i < code.length(); i++) {
            g.setColor(getRandomColor());
            g.drawString(String.valueOf(code.charAt(i)), 20 * i + 10, HEIGHT / 2 + FONT_SIZE / 2);
        }
    }
    
    /**
     * 获取随机颜色
     *
     * @return 随机颜色
     */
    private static Color getRandomColor() {
        return new Color(RANDOM.nextInt(180), RANDOM.nextInt(180), RANDOM.nextInt(180));
    }
    
    /**
     * 验证码信息类
     */
    public static class CaptchaInfo {
        private final BufferedImage image;
        private final String code;
        
        public CaptchaInfo(BufferedImage image, String code) {
            this.image = image;
            this.code = code;
        }
        
        public BufferedImage getImage() {
            return image;
        }
        
        public String getCode() {
            return code;
        }
    }
} 